import { fromError, initAxios } from '../common';
import type { ApiResponse } from '../common';

const client = initAxios();

export type LoginFormData = {
  username: string;
  password: string;
};

export async function login(data: LoginFormData) {
  try {
    const resp = await client.post<ApiResponse<boolean>>('/api/auth/login', data);
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function reg(data: LoginFormData) {
  try {
    const resp = await client.post<ApiResponse<boolean>>('/api/auth/reg', data);
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export type UserRole = 'admin' | 'mod' | 'user';

export type SimpleUserInfo = {
  id: number;
  name: string;
  avatar: string | null;
  role: UserRole;
}

export type UserInfo = SimpleUserInfo & {
  bio: string;
  created_at: string;
  last_login: string;
};
export async function currentUser() {
  try {
    const resp = await client.get<ApiResponse<UserInfo>>('/api/auth/me');
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function logout() {
  try {
    const resp = await client.delete<ApiResponse<boolean>>('/api/auth/logout');
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function getUserInfoById(id: number) {
  try {
    const resp = await client.get<ApiResponse<UserInfo>>('/api/auth/' + id);
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function setUserRole(id: number, role: UserRole) {
  try {
    const resp = await client.put<ApiResponse<boolean>>('/api/auth/' + id + '/role', { role });
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function uploadUserAvatar(avatar: File) {
  try {
    const resp = await client.put<ApiResponse<UserInfo>>('/api/auth/me/avatar', avatar);
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function updateUserBio(bio: string) {
  try {
    const resp = await client.put<ApiResponse<UserInfo>>('/api/auth/me/bio', { bio });
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export async function deleteUser(id: number) {
  try {
    const resp = await client.delete<ApiResponse<boolean>>('/api/auth/' + id);
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}

export type UserInfoForList = SimpleUserInfo & {
  deleted: boolean;
  bio: string;
  created_at: string;
}

export type UserListResponse = {
  total: number;
  users: UserInfoForList[];
};

export function queryUserListByKeywordPrepare(keyword: string) {
  return async function queryUserListByKeyword(page: number) {
    try {
      const params = { keyword, page, pageSize: 20 };
      const resp = await client.get<ApiResponse<UserListResponse>>('/api/auth/query', { params });
      return resp.data;
    } catch (error) {
      return fromError(error);
    }
  }
}

export async function adminQueryUserListByKeyword(keyword: string, page: number, pageSize: number) {
  try {
    const params = { keyword, page, pageSize, all: 1 };
    const resp = await client.get<ApiResponse<UserListResponse>>('/api/auth/query', { params });
    return resp.data;
  } catch (error) {
    return fromError(error);
  }
}
